const WXAPI = require('../../utils/api')
const AUTH = require('../../utils/auth')
const CONFIG = require('../../config.js')

Page({
	data: {
    // mediaInfo: JSON.stringify({
    //   image_resources: [
    //     { url: 'https://picasso-static.xiaohongshu.com/fe-platform/0b39a727f2adcd5d4dd07c94aac10717017f8a4a.png' },
    //   ],
    // })
  },
	onLoad() {
    this.readConfigVal()
    // 补偿写法
    getApp().configLoadOK = () => {
      this.readConfigVal()
    }
	},
  onShow() {
  },
  readConfigVal() {
    const mallName = xhs.getStorageSync('mallName')
    if (!mallName) {
      return
    }
    let subDomain = xhs.getExtConfigSync().subDomain
    if (!subDomain) {
      subDomain = CONFIG.subDomain
    }

    this.setData({
      subDomain,
      version: CONFIG.version,
      uid: xhs.getStorageSync('uid'),
      wifi: xhs.getStorageSync('wifi'),
      online_pay: xhs.getStorageSync('online_pay'),
      quhao: xhs.getStorageSync('quhao'),
      show_copyright: xhs.getStorageSync('show_copyright'),
      userBaseInfo: getApp().globalData.userBaseInfo
    })
  },
  onShareAppMessage(options) {
    // 用户点击右上角转发
    return {
      title: '"' + xhs.getStorageSync('comName') + '" ' + xhs.getStorageSync('share_profile'),
      path: '/pages/index/index',
      query: 'inviter_id='+ xhs.getStorageSync('uid')
    }
  },
  onShareTimeline(options) {
    // 用户点击右上角转发到朋友圈
    return {
      title: '"' + wx.getStorageSync('comName') + '" ' + wx.getStorageSync('share_profile'),
      path: '/pages/index/index',
      query: 'inviter_id='+ xhs.getStorageSync('uid')
    }
  },
  onShareChat(options) {
    // 用户点击右上角转发到微信群组
    return {
      title: '"' + wx.getStorageSync('comName') + '" ' + wx.getStorageSync('share_profile'),
      path: '/pages/index/index',
      query: 'inviter_id='+ xhs.getStorageSync('uid')
    }
  },
  async handleGetUserInfo(e) {
    if (e.detail.errMsg != 'getUserInfo:ok') {
      xhs.showToast({
        title: e.detail.errMsg,
        icon: 'none'
      })
      return
    }
    res = await WXAPI.modifyUserInfo({
      token: xhs.getStorageSync('token'),
      avatarUrl: e.detail.userInfo.avatarUrl,
      nick: e.detail.userInfo.nickName,
      gender: e.detail.userInfo.gender + 1,
    })
    if (res.code != 0) {
      xhs.showToast({
        title: res.msg,
        icon: 'none'
      })
      return
    }
    xhs.showToast({
      title: '登陆成功',
    })
    await getApp().updateUserInfoAndMobile()
    this.setData({
      userBaseInfo: getApp().globalData.userBaseInfo
    })
  },
  openSetting() {
    xhs.openSetting()
  },
  downloadpdf() {
    this.setData({
      loading: true
    })
    xhs.downloadFile({
      url: 'https://ns.s2m.cc/yuxin.pdf', //仅为示例，并非真实的资源
      success: res => {
        // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
        if (res.statusCode === 200) {
          xhs.openDocument({
            filePath: res.tempFilePath,
            fileType: 'pdf',
            success: res => {
              this.setData({
                loading: false
              })
            },
            fail: err => {
              this.setData({
                loading: false
              })
            },
          })
        }
      },
      fail: err => {
        xhs.showToast({
          title: '网络异常',
          icon: 'none'
        })
        this.setData({
          loading: false
        })
      }
    })
  },
})